package com.itheima.service.db.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.domain.db.TopicQuestion;
import com.itheima.mapper.QuestionForSoulMapper;
import com.itheima.service.db.OptionSoulService;
import com.itheima.service.db.QuestionForSoulService;
import com.itheima.vo.OptionsVo;
import com.itheima.vo.QuestionsVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
@RequiredArgsConstructor
public class QuestionForSoulServiceImpl extends ServiceImpl<QuestionForSoulMapper, TopicQuestion> implements QuestionForSoulService {

    private final OptionSoulService optionSoulService;

    @Override
    public List<QuestionsVo> findQuestions() {
        List<TopicQuestion> list = lambdaQuery().list();
        List<QuestionsVo> QuestionsVos = new ArrayList<>();
        for (TopicQuestion topicQuestion : list) {
            QuestionsVo questionsVo = new QuestionsVo();
            questionsVo.setId(topicQuestion.getId());
            questionsVo.setQuestion(topicQuestion.getContent());
            QuestionsVos.add(questionsVo);
        }
            return QuestionsVos;
    }

    @Override
    public List<QuestionsVo> findQuestionsByTopicId(String id) {
        //根据topic的id查询对应的问题
        List<TopicQuestion> list = lambdaQuery().eq(TopicQuestion::getTopicId, id).list();
        List<QuestionsVo> QuestionsVos = new ArrayList<>();

        if(list == null || list.isEmpty()){
            return Collections.emptyList();
        }
        //组装QuestionsVo
        for (TopicQuestion tq : list) {
            QuestionsVo questionsVo = new QuestionsVo();
            questionsVo.setId(tq.getId());
            questionsVo.setQuestion(tq.getContent());
            //根据问题id查询对应选项option
            String id1 = tq.getId();
            List<OptionsVo> optionsVos = optionSoulService.findOptionsByQuestionId(id1);
            if(optionsVos == null || optionsVos.isEmpty()){
                questionsVo.setOptions(Collections.emptyList());
            }
            questionsVo.setOptions(optionsVos);
            QuestionsVos.add(questionsVo);
        }
        return QuestionsVos;
    }

    @Override
    public int findDimensionalityById(String questionId) {
        TopicQuestion one = lambdaQuery().eq(TopicQuestion::getId, questionId).one();
        int characterType = Integer.parseInt(one.getCharacterType());
        return characterType;
    }
}
